

const mongoose = require('mongoose');

var express = require('express')
// 注意这里的声明
var JobDesire = express()
// const controller= require("../../controller/controllerenter")

const {createResponse}= require("../../util/createResponse/index");
const {businessCodes}= require("../../code/businessCodes/index");
const { JobDesireService } = require('../../service/impl/JobDesireService/index');
// const { JobDesire_bean } = require('../../beans/impl/JobDesire_bean/index');


JobDesire.post('/post/JobDesire/add', async (req,res) => {
  let result
  try {
    let news  = req.body.news
    result =  await JobDesireService.getIns().create(news)
  } catch (error) {
    console.log(error);
    createResponse(res,500,businessCodes.b2)
    return
  }
    createResponse(res,200,businessCodes.b1,result)
  })
  
  JobDesire.post('/post/JobDesire/delete', async (req,res) => {
    let result
    try {
      let  id  = req.body.conditions.id
      result =  await JobDesireService.getIns().findByIdAndDelete(id)
    } catch (error) {
      console.log(error);
      createResponse(res,500,businessCodes.b2)
      return
    }
      createResponse(res,200,businessCodes.b1,result)
    })


  // 添加求职信息
  JobDesire.post('/post/JobDesire/findOneupdate', async (req,res) => {
    let result
    try {
      let conditions = {
        "userid":req.body.conditions.userid,
        "desireProfession":req.body.updates.desireProfession
      }
    
      let updates = {
        "professionType":req.body.updates.professionType,
        "isShow":true,
        "desireCity":req.body.updates.desireCity
      }
      let options ={
        "new":true,
        "upsert":true,
        "setDefaultsInInsert":true 
      }
      result =  await JobDesireService.getIns().findOneAndUpdate(conditions,updates,options)
    } catch (error) {
      console.log(error);
      createResponse(res,500,businessCodes.b2)
      return
    }
      createResponse(res,200,businessCodes.b1,result)
    })
  
  JobDesire.post('/post/JobDesire/update', async (req,res) => {
    let result
    try {
      let  id  = req.body.conditions.id
      let updates = req.body.updates
      result =  await JobDesireService.getIns().findByIdAndUpdate(id,updates)
    } catch (error) {
      console.log(error);
      createResponse(res,500,businessCodes.b2)
      return
    }
      createResponse(res,200,businessCodes.b1,result)
    })
  
  //  根据用户id.查询该用户的求职需求
  JobDesire.post('/post/JobDesire/queryByUserid', async (req,res) => {
    let result
    try {
      let conditions={
        "userid":req.body.conditions.userid
      }
      let fills ={
        "key1":{
          "path":'desireProfession'
        }

      }
      result =  await JobDesireService.getIns().find(conditions,null,fills)
    }catch (error) {
      console.log(error);
      createResponse(res,500,businessCodes.b2)
      return
    }
      createResponse(res,200,businessCodes.b1,result)
    })
  
  module.exports= JobDesire